/*
 * PageAnalyzer.java
 * 
 * TCSS 422: Project-1
 */
import java.util.List;
import java.util.Map;
import java.util.ArrayList;
import java.util.HashMap;

/**
 * @author Shaoni Sinha shaoni@uw.edu
 * @version 25th January, 2014
 *
 */
public class PageAnalyzer {
    
    /** Map of words and their count. */
    private Map<String, Integer> myWordFrequencyMap;
    
    /** Count of pages retrieved. */
    private int numOfPages;
    
    /** Count of all words. */
    private int numOfWords;
    
    /** Count of URLs. */
    private int numOfURLs;
    
    /**
     * Constructor that initializes with everything
     * empty and values set to 0.
     */
    public PageAnalyzer() {
        myWordFrequencyMap = new HashMap<String, Integer>();
        numOfPages = 0;
        numOfWords = 0;
        numOfURLs = 0;
    }
    
    /**
     * Increases the count for each keyword that
     * is found within the given list.
     * 
     * @param text The text to search through.
     */
    public void processWordList(final List<String> theTextDoc, 
                                final List<String> wordList) {
        String theWord;
        
        if (theTextDoc == null || theTextDoc.size() == 0)
            return;
        
        for (int i = 0; i < theTextDoc.size(); i++) {
            if (wordList.contains(theTextDoc.get(i))) {
                theWord = theTextDoc.get(i);
                if(!myWordFrequencyMap.containsKey(theWord))
                {
                    myWordFrequencyMap.put(theWord, 1);
                }
                else
                {
                    myWordFrequencyMap.put(theWord, myWordFrequencyMap.get(theWord) + 1);
                }
            }
            numOfWords++;
        }
        numOfPages++;
    }
    
    /**
     * Counts the number of URLs.
     * 
     * @param theListOfURL List of URLs.
     */
    public void countURL(final List<String> theListOfURL) {
        numOfURLs = numOfURLs + theListOfURL.size();
    }
    
    /**
     * @return the no. of occurrences of a selected word
     * @param the word whose occurrence is to be returned.
     */
    public int getTotalOccurrenceOfSelectedWord(final String theOtherWord) {
        if(myWordFrequencyMap.containsKey(theOtherWord))
            return myWordFrequencyMap.get(theOtherWord);
        return 0;
        }
    
    
    /**
     * @return returns average number of occurrence 
     * of each selected word.
     **/
    public int getAvgOccurrenceOfSelectedWord(final String theWord) {
        if(numOfPages <= 0)
            return 0;
        return (getTotalOccurrenceOfSelectedWord(theWord)/numOfPages);
    }
    
    
    /**
     * @return the total number of pages crawled.
     */
    public int getNumOfPagesCrawled() {
            return numOfPages;
    }
    
    /**
     * @return the average amount of words per page.
     */
    public int getAvgNumOfWords() {
        int avgNumOfWords = 0; 
            if (numOfPages > 0) {
                avgNumOfWords =  numOfWords / numOfPages;
            }
            return avgNumOfWords;
    }
    
    /**
     * @return the average amount of URLs per page.
     */
    public int getAvgNumOfURLs() {
        int avgNumOfURLs = 0;
            if (numOfPages > 0) {
                avgNumOfURLs =  numOfURLs/numOfPages;
            }
            return avgNumOfURLs;
            
            }
    }


